<?php

/**
 * ExtTreeNodeBase class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */
class ExtTreeNodeBase extends ExtNode
{

    private $_node_class_types = array('ExtTreeNode');
    private $swap = false;
    private $orgId = null;
    private $fakeId = null;

    public function init()
    {
        parent::init();
        $this->orgId = $this->getId();
        if(is_numeric($this->orgId))
        {
            $this->swap = true;
            $this->fakeId = md5(microtime());
            $this->setId($this->fakeId);
        }
    }

    private function swapId($result)
    {
        if($this->swap)
        {
            return str_replace($this->fakeId, $this->orgId, $result);
        }
        else
        {
            return $result;
        }
    }
    
    public function parseNode(ExtHTMLParserNode $node, ExtASTWalker $astwalker)
    {
        parent::parseNode($node, $astwalker);
        $this->parseQuickTip($node, $astwalker, 'qtipCfg', false);
        
        if (!$this->hasNodeChildren($node) && is_null($this->parent->getConfig('loader')))
            $this->setConfig('leaf', true);

        $nodes = $node->findPropertyByName('nodes');
        if (!is_null($nodes))
        {
            $astwalker->walk($nodes, $this, $this->_node_class_types);
            $this->setArrayConfig('children', $this->children);
            $nodes->shiftUp();
        }
        $node->shiftUp();
    }

    private function hasNodeChildren($node)
    {
        if (!is_array($node->children))
            return false;
        $result = false;
        foreach ($node->children as $node)
        {
            if ($node->exttag == 'nodes')
                $result = true;
        }
        return $result;
    }

    public function renderConfig($asobject=true)
    {
        $orgid = $this->getId();
        $swapid = false;
        $fakeid = null;
        if(is_numeric($orgid))
        {
            $fakeid = md5(microtime());
            $this->setId($fakeid);
        }

        if ($this instanceof ExtAsyncTreeNode)
        {
            $this->setConfig('nodeType', 'async');
        }

        if ($this->parent instanceof ExtTreePanelBase)
        {
            /*
             * we always create the tree nodes as lazy objects
             * based on experimentation the asobject=true rendered nodes
             * do not work. the reason for this is unclear.
             */
            $this->parent->setJavaScriptConfig('root',$this->swapId(parent::renderConfig(false)));
            return null;
        }
        elseif ($this->parent instanceof ExtTreeNodeBase)
        {
            $this->parent->children[] = $this->swapId(parent::renderConfig(false));
            return null;
        }
    }

}

?>
